package com.lwh.springboot.controller;

import com.lwh.springboot.service.IUserService;
import com.lwh.springboot.pojo.entity.User;
import com.lwh.springboot.util.PageInfo;
import com.lwh.springboot.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "/login";
    }
    @RequestMapping("/toRegist")
    public String toRegist(){
        return "/regist";
    }
    @RequestMapping("/login")
    @ResponseBody //返回json格式数据
    public Result login(String name, String password, String code, HttpSession session){  //先验证验证码
        String codeInSession= (String) session.getAttribute("codeInSession");
        if (!codeInSession.equalsIgnoreCase(code)){
            return Result.error("验证码错误");
        }
        User user=userService.login(name,password);
        if(user !=null){
            if (user.getStatus()==0){//禁用
                return Result.error("该用户已经被禁用");
            }
            session.setAttribute("user",user);
            return Result.ok("登录成功",user);
        }else {
            return Result.error("登录失败");
        }
    }

    //注册普通用户
    @RequestMapping("/regist")
    @ResponseBody //返回json格式数据
    public Result regist(String name, String password, String phone){
        User user = new User();
        user.setName(name);
        user.setPassword(password);
        user.setRole(2);
        user.setPhone(phone);
        boolean result = userService.regist(user);
        if(result){
            return Result.ok("注册成功",user);
        }else {
            return Result.error("注册失败");
        }
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/toLogin";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(Integer id,Model model){
        User user=userService.selectById(id);
        model.addAttribute("user",user);
        return "/user/update";
    }
    @RequestMapping("/update")
    public String update(User user,HttpSession session) throws UnsupportedEncodingException {
        User loginUser=(User) session.getAttribute("user");
        userService.update(user);
        String encodedName = URLEncoder.encode(user.getName(), "UTF-8");
        if (loginUser.getRole()==2){
            return "redirect:/user/selectByPageById";
        }else {
            return "redirect:/user/selectByPage";
        }
    }
    @RequestMapping("toAdd")
    public String toAdd(){
        return "/user/add";
    }
    @RequestMapping("/add")
    public String add(User user){
        userService.add(user);
        return "redirect:/user/selectByPage";
    }
    @RequestMapping("/deleteById")
    public String deleteById(Integer id){
        userService.deleteById(id);
        return "redirect:/user/selectByPage";
    }
    @RequestMapping("/deleteAll")
    public String deleteAll(Integer[] ids) {
        userService.deleteAll(ids);
        return "redirect:/user/selectByPage";
    }
    @RequestMapping("/selectByPage")
    public String selectByPage(@RequestParam(defaultValue = "1") Integer pageNo,
                               @RequestParam(defaultValue = "5") Integer pageSize, Model model,String name, String phone, Integer status) {
        System.out.println("UserController.selectByPage");
        if (null == status || 3 == status) {
            status = null;
        }
        PageInfo pageInfo = userService.selectByPage(pageNo, pageSize,name, phone, status);

        // 将搜索条件的值添加到模型中
        model.addAttribute("searchName", name);
        model.addAttribute("searchPhone", phone);
        model.addAttribute("status", status);
        //把pageInfo数据放到内存里面
        model.addAttribute("pageInfo", pageInfo);
        //转发到user_list界面展示
        return "/user/list";
    }

    @RequestMapping("/selectByPageById")
    public String selectByPageById(@RequestParam(defaultValue = "1") Integer pageNo,
                               @RequestParam(defaultValue = "5") Integer pageSize,
                                   Model model,String name,HttpSession session) {

        User user = (User) session.getAttribute("user");
        System.out.println("UserController.selectByPageById");
        PageInfo pageInfo = userService.selectByPageById(pageNo, pageSize,user.getId());

        //把pageInfo数据放到内存里面
        model.addAttribute("pageInfo", pageInfo);
        //转发到user_list界面展示
        return "/user/list";
    }
    @RequestMapping("/selectAll")
    public String selectAll(Model model) {
        // soutm
        System.out.println("UserController.selectAll");
        List<User> list = userService.selectAll();
        //把list数据放到内存里面
        model.addAttribute("list", list);
        //转发到user_list界面展示
        return "/user/list";
    }
}
